import axios from 'axios'

class Http {
  constructor(opts = {}) {
    // 一旦有全域名，则就可以不走代理，线上也没有代理
    // opts.baseURL = process.env.NODE_ENV == 'dev' ? '' : process.env.VUE_APP_HOST
    opts.timeout = opts.timeout || 20000
    if (opts.headers) {
      opts.headers.aa = opts.headers.aa || 'admin'
    } else {
      opts.headers = {}
    }
    // 添加到成员属性
    this.opts = opts
  }

  getInstance(opts = {}) {
    // 对象合并
    // let options = { ...this.opts, ...opts }
    let options = Object.assign({}, this.opts, opts)
    return axios.create(options)
  }

  get(url, opts = {}) {
    return this.getInstance(opts).get(url)
  }

  post(url, data = {}, opts = {}) {
    return this.getInstance(opts).post(url, data)
  }

}


export default new Http()
